﻿myApp.controller('UsefulInfoDetailController', function ($scope, $http, $routeParams, $filter, NgMap,$location, $rootScope) {

    //$scope.share = {};
    $scope.isSearch = true;
    $scope.isAndroid = $rootScope.isAndroid;



    var BASE_URL;
    if ($location.host() == "localhost") {
        BASE_URL = $location.protocol() + "://" + "localhost:" + $location.port();
    } else {
        BASE_URL = $location.protocol() + "://" + $location.host();
    }
    $scope.current_path_url = $location.absUrl();


    pageInit();
    function pageInit() {
        console.log("RUNNING...");
        var url = 'api/UsefulInfo/GetUsefulInfo/' + $routeParams.id;
        $http.get(url).success(function (data) {
            console.log("USEFUL INFO: " + JSON.stringify(data));
            $scope.UsefulInfo = data.UsefulInfo;
            $scope.UsefulPhoto = data.pictureList[0].pictureURL;

            $scope.UsefulDetails = data.UsefulInfo.Details.replace(new RegExp('\r?\n', 'g'), '<br />');

            $scope.slides = data.pictureList;


            $scope.shareTitle = data.UsefulInfo.Title;
            $scope.shareDescription = data.UsefulInfo.Details;
            $scope.shareURL = BASE_URL + "/info/usefulInfo/details/" + $routeParams.id;
            $scope.shareImageURL = data.pictureList[0].pictureURL || "http://www.penangrealty.my/images/penang realty,Apartment,house,condominium,terrace house,semi d,detached,bungalow,high end luxury properties,landed properties,gated guarded house.png";

            $scope.googleShareURL = encodeURI(encodeURI($scope.shareURL));
            $scope.isSearch = false;

        }).error(function (err) {
            alert("There is problem loading. Please try again later.");
            $scope.isSearch = false;

        });
    }



    $scope.submitForm = function (obj) {
        var url = 'https://script.google.com/macros/s/AKfycbwcomu5o2CIbkO1lCjyZhDrF_dejSPWi0VAj9knYKYRr3jHDUY/exec';
        $http({
            method: 'POST',
            url: url,
            data: $(obj.target).serialize(),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function (data) {
            alert("Thanks for Subscribing to our Newsletter!");
            $scope.subscription_email = "";
        });
    }



});